﻿using System;
using System.Collections.Generic;
using System.Text;
using SH;
namespace MTS
{
    class mtSysManage : mtInterface,  ImtSysManage
    {

     public   mtSysManage()
            :base("mtSysManage")
        {

        }

       protected  override  shErrorCode _Initialize() 
        {
            BindEpic(emProgramType.emPT_SMS, ImtSysManageServerCallback._classID);
            BindEpic(emProgramType.emPT_SMS, ImtSMSServerCallback._classID);

            return shErrorCode.sh_ec_Success;
        }

        shErrorCode ImtSysManage.Start()
        {
           var host = GetVHost();

          return  host.Start();

        }

        shErrorCode ImtSysManage.StatusNotify(uint nStatusCode)
        {
            m_pHost.RunEnv.RunStatus = nStatusCode;

            MTSSerializer sc = new MTSSerializer();

            sc.serialize(ref nStatusCode);

            return __InvokeOneWay(ImtSMSServerCallback._classID, ImtSMSServerCallback.SMSServerStatusNotify, sc);
        }

        shErrorCode ImtSysManage.Stop()
        {
            var host = GetVHost();

            return host.Stop();
        }

        shErrorCode ImtSysManage.ErrorNotify(shErrorCode nErrorCode)
        {
            MTSSerializer sc = new MTSSerializer();
            sc.serialize(ref nErrorCode);

            return __InvokeOneWay(ImtSMSServerCallback._classID, ImtSMSServerCallback.SMSServerErrorNotify, sc);
        }

        shErrorCode ImtSysManage.EventNotify(uint nRank, uint nEventId, uint nEventSubId, string describe)
        {
            MTSSerializer sc = new MTSSerializer();
            sc.serialize(ref nRank);
            sc.serialize(ref nEventId);
            sc.serialize(ref nEventSubId);
            sc.serialize(ref describe);

            var ts = DateTime.Now.Ticks;

            sc.serialize(ref ts);

            return __InvokeOneWay(ImtSMSServerCallback._classID, ImtSMSServerCallback.SMSServerEventNotify, sc);
        }

        shErrorCode ImtSysManage.GetCredential(out mtCredential crd)
        {
            MTSSerializer sc = new MTSSerializer();

            if (m_pHost.RunEnv.IsAutoLogon)
            {
                if (m_pHost.RunEnv.Credential != null)
                {
                    crd = new mtCredential(m_pHost.RunEnv.Credential);
                    return shErrorCode.sh_ec_Success;
                }
                else
                    crd = null;

                return shErrorCode.sh_ec_UserNotLogon;
            }
            else
            {
                crd = null;
                return shErrorCode.sh_ec_UserNotLogon;
            }
          
            //var ec = __InvokeTwoWay(0, 7, ref sc);

            //if (ec != shErrorCode.sh_ec_Success)
            //{
            //    crd = null;
            //    return ec;
            //}

            //crd = new mtCredential();
            //sc.serialize(crd);

            //return ec;
        }

        shErrorCode ImtSysManage.GetCredential(string uuid, out mtCredential crd)
        {
            MTSSerializer sc = new MTSSerializer();

            sc.serialize(ref uuid);

            var ec = __InvokeTwoWay(ImtSMSServerCallback._classID, ImtSMSServerCallback.SMSServerGetCredential, ref sc);

            if (ec != shErrorCode.sh_ec_Success)
            {
                crd = null;
                return ec;
            }

            crd = new mtCredential();
            sc.serialize(crd);

            return ec;
        }

        shErrorCode ImtSysManage.GetGSystemRunStatus(out mtGSystemStatus info)
        {

            MTSSerializer sc = new MTSSerializer();
         
            var ec = __InvokeTwoWay(ImtSysManageServerCallback._classID, ImtSysManageServerCallback.SysManageGetGSystemRunStatus, ref sc);

            if(ec != shErrorCode.sh_ec_Success)
            {
                info = null;
                return ec;
            }

            info = new mtGSystemStatus();
            sc.serialize(info);

            return ec;
        }

        shErrorCode ImtSysManage.GetLastError()
        {
            MTSSerializer sc = new MTSSerializer();

            var ec = __InvokeTwoWay(0, 8, ref sc);

            if (ec != shErrorCode.sh_ec_Success)
            {
                return ec;
            }

           sc.serialize(ref ec);

            return ec;
        }

        uint ImtSysManage.GetRunStatus()
        {
            uint runStatus = 0;

            MTSSerializer sc = new MTSSerializer();

            var ec = __InvokeTwoWay(0, 9, ref sc);

            if (ec != shErrorCode.sh_ec_Success)
            {
                return 0;
            }

            sc.serialize(ref runStatus);

            return runStatus;
        }

        shErrorCode ImtSysManage.GetResourceString(string lang, List<uint> vecrId, out List<string> vecResource)
        {
            MTSSerializer sc = new MTSSerializer();

            var ec = __InvokeTwoWay(ImtSMSServerCallback._classID, ImtSMSServerCallback.SMSServerGetResourceString, ref sc);

            if (ec != shErrorCode.sh_ec_Success)
            {
                vecResource = null;
                return ec;
            }

            vecResource = new List<string>();

            sc.serialize(ref vecResource);

            return ec;
        }
        shErrorCode ImtSysManage.GStationRunStatusNotify(mtGStationRunStatusInfo info)
        {
            MTSSerializer sc = new MTSSerializer();
            sc.serialize(info);

            return __InvokeOneWay(ImtSysManageServerCallback._classID, ImtSysManageServerCallback.SysManageGStationRunStatusNotify, sc);
        }

        shErrorCode ImtSysManage.GWFTaskProcessInfoNotify(mtGWFTaskProcessInfo info)
        {
            MTSSerializer sc = new MTSSerializer();
            sc.serialize(info);

            return __InvokeOneWay(ImtSysManageServerCallback._classID, ImtSysManageServerCallback.SysManageGWFTaskProcessInfoNotify, sc);
        }

        shErrorCode ImtSysManage.LogOn(string userName, string password)
        {
            var host = GetVHost();

            return host.LogOn(userName,password);
        }

        shErrorCode ImtSysManage.LogOut()
        {
            var host = GetVHost();

            return host.LogOut();
        }

        shErrorCode ImtSysManage.OperationLogNotify(uint nRank, uint nEventId, uint nEventSubId, mtOperationInfo operationInfo)
        {
            MTSSerializer sc = new MTSSerializer();
            sc.serialize(ref nRank);
            sc.serialize(ref nEventId);
            sc.serialize(ref nEventSubId);
            sc.serialize(operationInfo);
   
            var ts = DateTime.Now.Ticks;

            sc.serialize(ref ts);

            return __InvokeOneWay(ImtSMSServerCallback._classID, ImtSMSServerCallback.SMSServerOperationLogNotify, sc);

        }
      
    }
    }
